package jp.co.systemi.asp.utils;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

public class StringUtils {
    private static final Logger log = Logger.getLogger(StringUtils.class.getName());
    public static String capitalize(String str){
        if(org.datanucleus.util.StringUtils.isEmpty(str)){
            return str;
        }
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }
    public static String mergeTemplate(String templateVm, Map<String, Object> vars){

        /*
         * create a new instance of the engine
         */

        VelocityEngine ve = new VelocityEngine();
        /*
         * configure the engine. In this case, we are using ourselves as a
         * logger (see logging examples..)
         */

        /*
         * initialize the engine
         */
        /* first, we init the runtime engine. */

        Properties p = new Properties();
        p.setProperty("input.encoding", "UTF-8");
        p.setProperty("runtime.log", "velocity.log");
        p.setProperty("resource.loader", "class");
        p.setProperty("class.resource.loader.description",
                "Velocity Classpath Resource Loader");
        p
                .setProperty("class.resource.loader.class",
                        "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

        try {
            ve.setProperty("runtime.log.logsystem.class",
                    "org.apache.velocity.runtime.log.NullLogChute");

            ve.init(p);
        } catch (Exception e) {
            log.info(e.getLocalizedMessage());
        }
        Template t = null;
        try {
            t = ve.getTemplate(templateVm);
        } catch (ResourceNotFoundException e) {
            log.info(e.getLocalizedMessage());
        } catch (ParseErrorException e) {
            log.info(e.getLocalizedMessage());
        } catch (Exception e) {
            log.info(e.getLocalizedMessage());
        }
        VelocityContext context = new VelocityContext();
        for (String key : vars.keySet()) {
            context.put(key, vars.get(key));
        }

        StringWriter sw = new StringWriter();
        try {
            t.merge(context, sw);
        } catch (ResourceNotFoundException e) {
            log.info(e.getLocalizedMessage());
        } catch (ParseErrorException e) {
            log.info(e.getLocalizedMessage());
        } catch (MethodInvocationException e) {
            log.info(e.getLocalizedMessage());
        } catch (IOException e) {
            log.info(e.getLocalizedMessage());
        }
        String msgBody = sw.toString();
        log.info("output : " + msgBody);
        return msgBody;
    }
}
